/*
 * Copyright 2009 Vladimir Ritz Bossicard
 *
 * This file is part of OraDoc.
 *
 * OraDoc is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * Version     : $Revision: 20 $
 * Last edit   : $Date: 2009-03-25 12:43:30 +0000 (Wed, 25 Mar 2009) $
 * Last editor : $Author: vbossica $
 */
package org.oradoc.structures.outputters;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.CharUtils;
import org.oradoc.outputters.AbstractHtmlOutputter;
import org.oradoc.outputters.ModelOutputter;
import org.oradoc.structures.dao.ViewTO;
import org.oradoc.utils.freemarker.HtmlOutputTemplate;

/**
 *
 * @author Vladimir Ritz Bossicard
 */
public class ViewsHtmlOutputter extends AbstractHtmlOutputter implements ModelOutputter {

    private final static int COLUMNS = 3;

    @SuppressWarnings("unchecked")
    public void output(final Map<String, Object> model) throws Exception {
        List<ViewTO> views = (List<ViewTO>) model.get("views");

        List<String> keys = new ArrayList<String>();
        List<String> tmp = new ArrayList<String>();
        for (ViewTO table : views) {
            char first = table.getName().charAt(0);
            String key = !CharUtils.isAsciiAlpha(first) ? "Others" : CharUtils.toString(first);
            if (!keys.contains(key)) {
                keys.add(key);
                tmp.add("$$$" + key);
            }
            tmp.add(table.getName());
        }

        String[] names = orderNames(tmp);
        model.put("columns", COLUMNS);
        model.put("keys", keys);
        model.put("names", names);

        new HtmlOutputTemplate(model, this.cfg, "views.html.ftl").process(this.outDir, "views.html");

        // group the views alphabetically
        new HtmlOutputTemplate(model, this.cfg, "views-sorted.html.ftl").process(this.outDir, "views-sorted.html");

        File viewDir = new File(this.outDir, "views");
        viewDir.mkdirs();
        for (ViewTO view : views) {
            model.put("view", view);
            new HtmlOutputTemplate(model, this.cfg, "view.html.ftl").process(viewDir, view.getName().toLowerCase() + ".html");
        }

        // copies the common files
        copyCommonFiles();
    }

}
